package cn.cmcc.intertelecom.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.cmcc.intertelecom.bean.Department;
import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.bean.User;
import cn.cmcc.intertelecom.service.DepartmentService;
import cn.cmcc.intertelecom.service.RoleGroupService;
import cn.cmcc.intertelecom.service.UserService;
import cn.cmcc.intertelecom.utils.ArchivesLog;

@SessionAttributes(names="currentPage")
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private RoleGroupService roleGroupService;
	
	/**
	 * 根据用户的id获取用户信息
	 * @param user
	 * @return
	 */

	@RequestMapping(value="/person/{id}" ,method=RequestMethod.GET)
	public String getPersonById(@PathVariable(value="id")Integer id,@RequestParam("target")String target
			, Map<String,Object> map){
	//	System.out.println(id);
	//	System.out.println(target);
		//获取用户
		User user =userService.getUserById(id);
		if("person-revise".equals(target)){
			List<Department> depts = departmentService.selectAllDepts();
			map.put("depts", depts);
		}
		map.put("user", user);

		return "person/"+target;
	}
	
	/**
	 * 删除数据  单一 和批量
	 * @param deptIds
	 * @return
	 */
	@ArchivesLog(operationPower="delete_user")
	@ResponseBody
	@RequestMapping(value="/person/{userids}" ,method=RequestMethod.DELETE)
	public Msg deleteUser(@PathVariable(value="userids")String userids){
		System.out.println(userids);
		if(userids.contains("-")){
			String[] str = userids.split("-");
			List<Integer> ids=new ArrayList<>();
			for (String string : str) {
				ids.add(Integer.parseInt(string));
			}

			//删除权限-角色 映射表里面的所有关于该角色的数据
			roleGroupService.deleteBatchByuserids(ids);
			
			userService.deleteBatch(ids);
		}else{
			//删除权限-角色 映射表里面的所有关于该员工的数据
			roleGroupService.deleteByuserid(Integer.parseInt(userids));
			//删除用户表中用户的数据
			userService.deleteDept(Integer.parseInt(userids));
		}
		return Msg.success();
	}
	
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	@ArchivesLog(operationPower="update_user")
	@ResponseBody
	@RequestMapping(value="/person" ,method=RequestMethod.PUT)
	public Msg updatePerson(User user){
		//System.out.println(user);
		//保存用户
		userService.updatePerson(user);
		return Msg.success();
	}
	
	/**
	 * 保存用户
	 * @param user
	 * @return
	 */
	@ArchivesLog(operationPower="new_user")
	@ResponseBody
	@RequestMapping(value="/person" ,method=RequestMethod.POST)
	public Msg savePerson(User user){
		//保存用户
		userService.savePerson(user);
		return Msg.success();
	}
	
	/**
	 * 获取全部用户的消息，包括所在部门已经部门的管理员
	 * @param pn
	 * @return
	 */
	
	@ResponseBody
	@RequestMapping(value="/person" ,method=RequestMethod.GET)
	public Msg getPerson(@RequestParam(value="pn",defaultValue="1")Integer pn,Map<String, Object>map){
		//获取第pn页，5条内容，默认查询总数count
		PageHelper.startPage(pn, 5);
		List<User> list = userService.selectAll();
		//System.out.println(list.get(0).getDepartment().getUser());
		//用PageInfo对结果进行包装
		PageInfo<User> page = new PageInfo<>(list);
	//将map放在session域中
		map.put("currentPage", page.getPageNum()) ;
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 *更具一致条件获取相应 人员的详细信息
	 * @param pn
	 * @return
	 */
	@ArchivesLog(operationPower="query_user")
	@ResponseBody
	@RequestMapping(value="/queryPerson/{pn}")
	public Msg queryPerson(@PathVariable(value="pn")Integer pn,User user){
		//System.out.println(user);
		//获取第1页，5条内容，默认查询总数count
	//	System.out.println(pn);
		if(pn == null || pn.equals("") || pn <=0){pn=1;};
		PageHelper.startPage(pn, 5);
		List<User> list = userService.selectUserByRequirement(user);
		//System.out.println(list.get(0).getDepartment().getUser());
		//用PageInfo对结果进行包装
		PageInfo<User> page = new PageInfo<>(list);
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * 根据部门id获取所有的用户
	 * @param deptId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/user/{deptId}",method=RequestMethod.GET)
	public Msg getPersonByDeptId(@PathVariable("deptId")Integer deptId){
		
		List<User> list = userService.getUsersByDeptId(deptId);
		
		return Msg.success().add("userList", list);
	}
	
	/**
	 * 分页显示所有的用户  只有user  没有外键
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/users",method=RequestMethod.GET)
	public Msg getAllUser(@RequestParam(value="pn",defaultValue="1")Integer pn){
		System.out.println(pn);
		PageHelper.startPage(pn, 5);
		List<User> list= userService.selectAllJustUser();
		PageInfo<User> pageInfo=new PageInfo<>(list);
		return Msg.success().add("pageInfo", pageInfo);
		
	}
}
